package zzps.exeed.kernel.core;

import zzps.exeed.kernel.holder.PluginHolder;

import java.util.Arrays;
import java.util.Comparator;

public interface Sort{
    default Class<?>[] after(){
        return new Class[0];
    }
    default Class<?>[] before(){
        return new Class[0];
    }
    default int index(){
        return 0;
    }

    default void conflict(){
        if (after().length >0 && before().length >0){
            boolean b = Arrays.stream(after()).anyMatch(after -> Arrays.stream(before()).anyMatch(before -> after == before));
            if (b){
                throw new IllegalArgumentException("before和after参数不能冲突" + this.getClass().toString());
            }
        }
    }


    static int sort(Sort first,Sort second){
            Class<?>[] firstAfter = first.after();
            Class<?>[] firstBefore = first.before();
            Class<?>[] secondAfter = second.after();
            Class<?>[] secondBefore = second.before();
            boolean b;
            if (firstAfter.length >0 && secondAfter.length >0){
                b = Arrays.stream(firstAfter).anyMatch(after -> Arrays.stream(secondAfter).anyMatch(before -> after == before));
                if (b){
                    throw new IllegalArgumentException("before和before参数不能冲突" + first.getClass().toString()+":"+second.getClass().toString());
                }
            }
            if (firstBefore.length >0 && secondBefore.length >0){
                b = Arrays.stream(firstBefore).anyMatch(after -> Arrays.stream(secondBefore).anyMatch(before -> after == before));
                if (b){
                    throw new IllegalArgumentException("after和after参数不能冲突" + first.getClass().toString()+":"+second.getClass().toString());
                }
            }
            b = Arrays.stream(firstBefore).anyMatch(clazz -> clazz == second.getClass());
            if (b){
                return -1;
            }
            b = Arrays.stream(firstAfter).anyMatch(clazz -> clazz == second.getClass());
            if (b){
                return 1;
            }
            b = Arrays.stream(secondBefore).anyMatch(clazz -> clazz == first.getClass());
            if (b){
                return 1;
            }
            b = Arrays.stream(secondAfter).anyMatch(clazz -> clazz == first.getClass());
            if (b){
                return -1;
            }
            if (first.index() > second.index()){
                return 1;
            }else if (first.index() < second.index()){
                return -1;
            }
            return 0;
    }
}
